package lt.liepasoft.locr.realization;

import java.util.List;
import java.util.Map;

import lt.liepasoft.locr.Recognizer;
import lt.liepasoft.locr.data.Symbol;
import lt.liepasoft.locr.data.SymbolImage;

public class SymbolRecognizer extends Recognizer {

	private Map <Character, SymbolImage> mSymbolsMap;
	
	public SymbolRecognizer(){
		this(new EncryptedSymbols().symbols);
	}
	
	public SymbolRecognizer(Map <Character, SymbolImage>symbolsMap){
		this.mSymbolsMap = symbolsMap;
	}
	
    public Character recognize(SymbolImage symbolImage){
        Character winner = '?'; 
        float maxRatio = 0;
        for (Character character: mSymbolsMap.keySet()){
            float ratio = similarityRate(
            		symbolImage, mSymbolsMap.get(character));
            if (ratio > maxRatio){
                winner = character;
                maxRatio = ratio;
            }
        }
        return winner;
    }

	@Override
	public List<Symbol> recognize(List<Symbol> symbols) {
		for (Symbol symbol: symbols)
			symbol.setCharacter(recognize(symbol.getImage()));
		return symbols;
	}
	
    public static Float similarityRate(SymbolImage s1, SymbolImage s2){
    	return s1.similarityRate(s2)*s2.similarityRate(s1);           
    }  

}
